//
//  UpdatePwdViewController.m
//  MoXiProject
//
//  Created by 周筱雯 on 2017/7/3.
//  Copyright © 2017年 周筱雯. All rights reserved.
//

#import "UpdatePwdViewController.h"
#import "MineViewModel.h"

@interface UpdatePwdViewController ()
@property (weak, nonatomic) IBOutlet UITextField *pwdOld;
@property (weak, nonatomic) IBOutlet UITextField *pwdNew;
@property (weak, nonatomic) IBOutlet UITextField *pwdAgain;

@property (weak, nonatomic) IBOutlet UIButton *updateClick;

@property (nonatomic,strong) MineViewModel * mineViewModel;

@end

@implementation UpdatePwdViewController

-(MineViewModel *) mineViewModel{
    if (!_mineViewModel) {
        _mineViewModel = [[MineViewModel alloc] init];
    }
    return _mineViewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self.navigationItem setTitle:@"修改密码"];
    [self.view setBackgroundColor:DEF_COLOR_F0F0F0];
    
    /**
     修改密码
     */
    [[self.updateClick rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        
        if ([self updateIfelse]) {
            
            [self updatePwdNetWork];
        }
    }];
    
}

-(void) updatePwdNetWork{
    
    [MXUtility showMBProgress:self.view message:LoadingText];
    
    [[self.mineViewModel updateUserPwdRequestParameter:self.pwdNew.text] subscribeNext:^(id x) {
        
        if ([x[@"errorcode"] integerValue] == 0) {
            
            [MXUtility showTips:@"修改密码成功~"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        
        [MXUtility hideMBProgress:self.view];
    }error:^(NSError *error) {
        
        [MXUtility hideMBProgress:self.view];
    }];
}


-(BOOL) updateIfelse{
    
    if ([MXUtility NSNULLString:self.pwdOld.text]) {
        [MXUtility showTips:@"请输入旧密码"];
        return false;
    }
    if (![[DEF_UserDefaults objectForKey:DEF_USERDEFAULTS_USERPASSWORD] isEqualToString:self.pwdOld.text]) {
        [MXUtility showTips:@"输入的旧密码不匹配"];
        return false;
    }
    if ([MXUtility NSNULLString:self.pwdNew.text]) {
        [MXUtility showTips:@"新密码不能为空"];
        return false;
    }
    if ([MXUtility NSNULLString:self.pwdAgain.text]) {
        [MXUtility showTips:@"再次密码不能为空"];
        return false;
    }
    if (![self.pwdNew.text isEqualToString:self.pwdAgain.text]) {
        [MXUtility showTips:@"两次输入密码不一致"];
        return false;
    }
    return true;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
